<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：IIdentity.class.php
 * @类功能: 解析身份证信息
 * @开发者: 陈旭林
 * @开发时间： 14-10-28
 * @版本：version 1.0
 */
namespace app\common\ext;

use app\common\enum\Sex;
use app\common\cache\BaseCache;
use app\common\cache\common\IAreaCache;
use app\common\enum\UserPoolBidType;
use app\common\enum\LoanApplyStatus;
use app\common\enum\CreditApplyStatus;
use app\common\ext\IDb;
use app\common\ext\IRedis;


class IHomeTotal extends BaseCache {
    /**
     * @功能：客户注册记录
     * @开发者：wdd
     */
    public static function getUserCustomerConList(){
        //参数的获取
        $list = IRedis::getCallBackString(self::XX_DATA_USER_TOTAL,null,function () {
            $date = get_date_time();
            $date_list = get_date_list($date, "-1 Month");

            //获取客户添加数量
            $where['_string_'] = "date_format(reg_time,'%Y-%m-%d')>date_format(now() - interval 1 month,'%Y-%m-%d')";
            $user_customer_list = IDb::getInstance('user')
                ->setDbFiled(["count(user_id) as num, date_format(reg_time,'%m/%d') as create_time"])
                ->setDbGroup("date_format(reg_time,'%Y-%m-%d')")
                ->setDbWhere($where)
                ->sel();

            $user_customer_data = array_column($user_customer_list,null,'create_time');

            //时间数据整理
            $user_customer_list = array();
            $data_arr = null;
            foreach ($date_list as $val) {
                if (empty($user_customer_data[$val])) {
                    $data_arr['create_time'] = $val;
                    $data_arr['num'] = 0;

                    //赋值
                    $user_customer_list[] = $data_arr;
                } else {
                    $user_customer_list[] = $user_customer_data[$val];
                }
            }

            return $user_customer_list;
        },600);

        return $list;
    }
    /**
     * @功能：合作商开标统计
     * @开发者：wdd
     */
    public static function getPoolBidList(){
        //参数的获取
        $list = IRedis::getCallBackString(self::XX_DATA_POOL_BID_TOTAL,null,function () {
            $date = get_date_time();
            $date_list = get_date_list($date, "-1 Month");

            //获取客户添加数量
            $where['user_pool_bid_type'] = UserPoolBidType::Cooperate;
            $where['_string_'] = "date_format(create_time,'%Y-%m-%d')>date_format(now() - interval 1 month,'%Y-%m-%d')";
            $user_pool_bid_list = IDb::getInstance('user_pool_bid')
                ->setDbFiled(["count(user_pool_bid_id) as num, date_format(create_time,'%m/%d') as create_time"])
                ->setDbGroup("date_format(create_time,'%Y-%m-%d')")
                ->setDbWhere($where)
                ->sel();

            $user_pool_bid_data = array_column($user_pool_bid_list,null,'create_time');

            //时间数据整理
            $user_pool_bid_list = array();
            $data_arr = null;
            foreach ($date_list as $val) {
                if (empty($user_pool_bid_data[$val])) {
                    $data_arr['create_time'] = $val;
                    $data_arr['num'] = 0;

                    //赋值
                    $user_pool_bid_list[] = $data_arr;
                } else {
                    $user_pool_bid_list[] = $user_pool_bid_data[$val];
                }
            }

            return $user_pool_bid_list;

        },600);

        return $list;
    }

    /**
     * @功能：注册用户统计
     * @开发者：陈旭林
     * @return string
     */
    public static function getProvinceList(){
        //取得缓存信息
        $info = IRedis::getCallBackString(self::XX_ADMIN_USER_AREA_NUM,null,function(){
            //取得用户列表数据
            $user_list = IDb::getInstance("user")->setDbFiled("province_id,count(user_id) as num")
                ->setDbWhere("province_id is not null")
                ->setDbGroup("province_id")
                ->setDbOrder("num desc")
                ->sel();

            //计算合计用户数据了
            $area_list = array();
            if(!empty($user_list) && is_array($user_list) && count($user_list) > 0){
                //取得名称
                foreach ($user_list as $item) {
                    //取得区域名称
                    $area_name = IAreaCache::getInfo($item['province_id'],"acronym");

                    //设置数据
                    $area_list[] = array("name"=>$area_name,"num"=>intval($item['num']));
                }
            }

            //返回值
            return $area_list;
        },3600);

        //返回值
        return $info;
    }

    /**
     * @功能：用户申请贷款与信用卡走势图
     * @开发者：陈旭林
     * @return string
     */
    public static function getUserApplyList(){
        //取得缓存信息
        $list = IRedis::getCallBackString(self::XX_USER_APPLY_LIST,null,function(){
            $date = get_date_time();
            $date_list = get_date_list($date, "-1 Month");

            //获取申请贷款数量
            $loan_where['loan_apply_status'] = LoanApplyStatus::apply;
            $loan_where['_string_'] = "date_format(loan_apply_time,'%Y-%m-%d')>date_format(now() - interval 1 month,'%Y-%m-%d')";
            $user_loan_list = IDb::getInstance('loan_apply')
                ->setDbFiled(["count(loan_apply_id) as loan_num, date_format(loan_apply_time,'%m/%d') as loan_apply_time"])
                ->setDbGroup("date_format(loan_apply_time,'%Y-%m-%d')")
                ->setDbWhere($loan_where)
                ->sel();

            $user_loan_data = array_column($user_loan_list,null,'loan_apply_time');

            //获取信用卡数量
            $credit_where['credit_apply_status'] = CreditApplyStatus::apply;
            $credit_where['_string_'] = "date_format(credit_apply_time,'%Y-%m-%d')>date_format(now() - interval 1 month,'%Y-%m-%d')";
            $user_credit_list = IDb::getInstance('credit_apply')
                ->setDbFiled(["count(credit_apply_id) as credit_num, date_format(credit_apply_time,'%m/%d') as credit_apply_time"])
                ->setDbGroup("date_format(credit_apply_time,'%Y-%m-%d')")
                ->setDbWhere($credit_where)
                ->sel();

            $user_credit_data = array_column($user_credit_list,null,'credit_apply_time');

            //整理数据
            $total_data = array();
            foreach($date_list as $key=>$date){
                //设置初期值
                $data['date_time'] = $date;
                $data['loan_num'] = 0;
                $data['credit_num'] = 0;

                //判断数据是否存在
                if(!empty($user_loan_data[$date]) && is_array($user_loan_data[$date])) {
                    $data["loan_num"] = intval($user_loan_data[$date]['loan_num']);
                }

                //判断数据是否存在
                if(!empty($user_credit_data[$date]) && is_array($user_credit_data[$date])) {
                    $data["credit_num"] = intval($user_credit_data[$date]['credit_num']);
                }

                //添加到数组中
                $total_data[] = $data;
            }

            return $total_data;

        },3600);

        return $list;
    }

    /**
     * @功能：用户申请贷款与信用卡数量
     * @开发者：wdd
     * @return string
     */
    public static function getUserApplyNumList(){
        //取得缓存信息
        $list = IRedis::getCallBackString(self::XX_USER_APPLY_NUM,null,function(){
            //取得数据
            $loan_apply_info = IDb::getInstance("loan_apply")
                ->setDbFiled("count(loan_apply_id) as loan_apply_num")
                ->setDbWhere(array('loan_apply_status'=>LoanApplyStatus::apply))
                ->row();

            $credit_apply_info = IDb::getInstance("credit_apply")
                ->setDbFiled("count(credit_apply_id) as credit_apply_num")
                ->setDbWhere(array('credit_apply_status'=>CreditApplyStatus::apply))
                ->row();

            //整理公司数据
            $list = array();
            if(empty($loan_apply_info['loan_apply_num'])){
                $list[] = array("name"=>"申请贷款数量","num"=>0);
            }else{
                $list[] = array("name"=>"申请贷款数量","num"=>intval($loan_apply_info['loan_apply_num']));
            }

            //整理公司业务员数据
            if(empty($credit_apply_info['credit_apply_num'])){
                $list[] = array("name"=>"申请信用卡数量","num"=>0);
            }else{
                $list[] = array("name"=>"申请信用卡数量","num"=>intval($credit_apply_info['credit_apply_num']));
            }

            //返回值
            return $list;
        },3600);

        //返回值
        return $list;
    }
}